package ch.bfh.mobicompsoda2.sms_sender.sender_service;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.telephony.SmsManager;
import android.util.Log;

public class SODA_SMS_NotificationBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = SODA_SMS_NotificationBroadcastReceiver.class.getName();
//    private static final int WAIT_TIME = 5 * 60 * 1000;
    private static final int WAIT_TIME = 1 * 10 * 1000;

    private String receiverNumber = "";
    private String spList = "";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(SODA_Send_SMS_Service.SODA_SEND_PENDINGINTENT_ACTION)) {
            // SMS sendResult
            // Switch von M.Schaer geklaut... THX

            receiverNumber = intent.getStringExtra(SODA_Send_SMS_Service.SEND_SMS_EXTRA_NUMBER);
            spList = intent.getStringExtra(SODA_Send_SMS_Service.SEND_SMS_EXTRA_SP_NAMES);
            Log.d(TAG, "SendNotification - receiveNumber: " + receiverNumber + " splist: " + spList);

            switch (getResultCode()) {
            case Activity.RESULT_OK:
                // SMS erfolgreich versendet.
                Log.d(TAG, "SMS versendet, Service wird gestopt");
                context.stopService(SODA_Send_SMS_Service.getStopSendIntent());
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                repeatingSolution(context, "RESULT_ERROR_GENERIC_FAILURE: "+intent.getStringExtra("errorCode"));
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                repeatingSolution(context, "RESULT_ERROR_NO_SERVICE");
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                repeatingSolution(context, "RESULT_ERROR_NULL_PDU");
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                repeatingSolution(context,"RESULT_ERROR_RADIO_OFF");
                break;
            }

        }
        
        if (intent.getAction().equals(SODA_Send_SMS_Service.SODA_DELIVERY_PENDINGINTENT_ACTION)) {
            Log.d(TAG, "DeliveryNotification - receiveNumber: " + receiverNumber + " splist: " + spList);
            Log.e(TAG, "DeliveryNotification - Isch mir mom. egau");
        }
        
    }

    private void repeatingSolution(Context context, String error) {
        Log.d(TAG, "SendError... "+ error);
        Log.d(TAG, "Try Later im MS"+ WAIT_TIME);
        context.stopService(SODA_Send_SMS_Service.getStopSendIntent());
        try2sendLater(context);
    }

    private void try2sendLater(Context context) {
        // TODO make this better!!
        Handler handler = new Handler();
        handler.postDelayed(new RepeatSender(context), WAIT_TIME);
    }

    private class RepeatSender implements Runnable {

        Context context = null;

        public RepeatSender(Context context) {
            this.context = context;
        }

        @Override
        public void run() {
            
            String[] shraredPreferencesnames = spList.split(",");
            Intent intent = SODA_Send_SMS_Service.getSendSMSIntent(receiverNumber, shraredPreferencesnames);
            context.sendBroadcast(intent);
            
        }
    }

}
